package youleche.com.sql.servlet.common;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.RandomAccessFile;
import java.util.List;

import youleche.com.sql.business.BTableInfo;
import youleche.com.sql.model.MTableField;

/**
 * 类生成工厂
 * @author Administrator
 *
 */
public class ClassGenerateFactory {
	/**
	 * 生成IDAO类
	 * @param path 类路劲
	 * @param tableId 表ID
	 * @param fieldList 字段列表
	 * @param packages 包名
	 * @return 返回提示字符串
	 * @throws Exception 异常
	 */
	public static String generateIdao(String path, String tableId, List<MTableField> fieldList, String packages) throws Exception{
		String tableName = createClass(path, tableId);
		String content = readClassContent(path, tableName);
		if("".equals(content)){
			//往里面写基础数据
			writeBaseContent(packages, tableName, path);
		}else{
			//往里面追加数据
		}
		
		return "";
	}
	
	/**
	 * 往类里面写基础数据
	 * @param packages 包名
	 * @param tableName 表名
	 * @param path 类路劲
	 * @throws Exception 异常
	 */
	private static void writeBaseContent(String packages, String tableName, String path) throws Exception{
		
		String packageStr = "package " + packages + ";\n\n";
		String className = "I" + tableName.substring(0, 1).toUpperCase() + tableName.substring(1, tableName.length());
		String clazz = "public interface "+ className +"{\n\n";
		String end = "}";
		String baseContent = packageStr + clazz + end;		
		RandomAccessFile raf = null;
		try{
			raf = new RandomAccessFile(new File(path + "\\" + className + ".java"), "rw");
			raf.writeBytes(baseContent);			
		}finally{
			if (raf != null) {
				 raf.close();
			}
		}		
	}
	
	/**
	 * 读取类内容
	 * @param path
	 * @param tableName
	 * @throws Exception
	 */
	private static String readClassContent(String path, String tableName) throws Exception{
		String idaoClassName = "I" + tableName.substring(0, 1).toUpperCase() + tableName.substring(1, tableName.length()) + ".java";
		FileReader fr = new FileReader(path + idaoClassName);
        BufferedReader br=new BufferedReader(fr);
        try {
        	
			while(br.readLine()!=null){
			    return br.readLine();
			}
		} finally{
			br.close();		
		}
		
		return "";
	}
	
	/**
	 * 创建类
	 * @param path 生成路径
	 * @param tableId 表ID
	 * @throws Exception 异常
	 */
	private static String createClass(String path, String tableId) throws Exception{
		String tableName = getTableNamebyTableId(tableId);
		String idaoClassName = "I" + tableName.substring(0, 1).toUpperCase() + tableName.substring(1, tableName.length()) + ".java";
		File file = new File(path + idaoClassName);
		if(!file.exists()){
			file.createNewFile();
		}
		
		return tableName;
	}
	
	/**
	 * 根据表ID得到表名
	 * @param tableId 表ID
	 * @return 返回表名
	 * @throws Exception 异常
	 */
	private static String getTableNamebyTableId(String tableId) throws Exception{
		BTableInfo infoHandler = new BTableInfo();
		
		return  infoHandler.getTableInfoByTableId(tableId).getTableName();
	}
}
